Interactive Map Example

Author

Hannah Loder

Interactive Maps with MapView Package

Code
# uncomment to run, then recomment it out so you don't run it every time

 census_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", install=TRUE, overwrite=TRUE)
Your original .Renviron will be backed up and stored in your R HOME directory if needed.
Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). 
To use now, restart R or run `readRenviron("~/.Renviron")`
[1] "2a6f8c21a30d3024e038d67d7d4eba647dc79cd4"
Code
#variables I'm looking at
myvars <- c(totalpop = "B01003_001",
            medincome = "B19013_001",
            medage = "B01002_001"
)
Code
#pull for MO counties
mo_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       state = "MO",
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
Code
mo_counties_withgeo
Simple feature collection with 115 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -95.7747 ymin: 35.99568 xmax: -89.09884 ymax: 40.61364
Geodetic CRS:  NAD83
First 10 features:
   GEOID                            NAME totalpopE totalpopM medincomeE
1  29033        Carroll County, Missouri      8564        NA      54967
2  29143     New Madrid County, Missouri     16658        NA      42370
3  29087           Holt County, Missouri      4295        NA      52500
4  29113        Lincoln County, Missouri     58689        NA      76622
5  29019          Boone County, Missouri    182170        NA      62653
6  29099      Jefferson County, Missouri    225913        NA      71285
7  29207       Stoddard County, Missouri     28812        NA      46052
8  29189      St. Louis County, Missouri   1001982        NA      72562
9  29195         Saline County, Missouri     23278        NA      49705
10 29031 Cape Girardeau County, Missouri     81086        NA      59252
   medincomeM medageE medageM                       geometry
1        4253    43.7     0.8 MULTIPOLYGON (((-93.75918 3...
2        3894    41.7     0.8 MULTIPOLYGON (((-89.97129 3...
3        4167    48.0     0.5 MULTIPOLYGON (((-95.55482 4...
4        2179    37.5     0.5 MULTIPOLYGON (((-91.26382 3...
5        1785    31.9     0.2 MULTIPOLYGON (((-92.57016 3...
6        2398    39.9     0.4 MULTIPOLYGON (((-90.77355 3...
7        4209    41.3     0.6 MULTIPOLYGON (((-90.25976 3...
8        1001    40.3     0.2 MULTIPOLYGON (((-90.73643 3...
9        4601    36.3     1.0 MULTIPOLYGON (((-93.49638 3...
10       2568    36.7     0.4 MULTIPOLYGON (((-89.8668 37...
Code
#all counties in the US
all_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Code
all_counties_withgeo
Simple feature collection with 3221 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -179.1489 ymin: 17.88328 xmax: 179.7785 ymax: 71.36516
Geodetic CRS:  NAD83
First 10 features:
   GEOID                              NAME totalpopE totalpopM medincomeE
1  20161              Riley County, Kansas     72602        NA      53296
2  19159             Ringgold County, Iowa      4739        NA      57700
3  30009            Carbon County, Montana     10488        NA      63178
4  16007           Bear Lake County, Idaho      6327        NA      60337
5  55011         Buffalo County, Wisconsin     13314        NA      61167
6  31185             York County, Nebraska     14164        NA      66337
7  08037            Eagle County, Colorado     55693        NA      91338
8  42129 Westmoreland County, Pennsylvania    355107        NA      64708
9  40079         Le Flore County, Oklahoma     48436        NA      43049
10 48053              Burnet County, Texas     48424        NA      65363
   medincomeM medageE medageM                       geometry
1        2489    25.5     0.1 MULTIPOLYGON (((-96.96095 3...
2        5058    44.3     1.0 MULTIPOLYGON (((-94.47167 4...
3        4261    50.7     0.9 MULTIPOLYGON (((-109.7987 4...
4        7039    38.9     1.1 MULTIPOLYGON (((-111.6345 4...
5        2352    46.5     0.5 MULTIPOLYGON (((-92.08384 4...
6        4128    39.5     1.2 MULTIPOLYGON (((-97.82629 4...
7        4058    37.8     0.8 MULTIPOLYGON (((-107.1137 3...
8        1350    47.1     0.2 MULTIPOLYGON (((-79.90487 4...
9        1869    38.8     0.3 MULTIPOLYGON (((-95.05996 3...
10       4694    44.7     0.3 MULTIPOLYGON (((-98.45924 3...
Code
#remove MOE columns - they all end with "M"
mo_counties_withgeo <- mo_counties_withgeo %>%
  select(-ends_with("M"))

mo_counties_withgeo
Simple feature collection with 115 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -95.7747 ymin: 35.99568 xmax: -89.09884 ymax: 40.61364
Geodetic CRS:  NAD83
First 10 features:
   GEOID                            NAME totalpopE medincomeE medageE
1  29033        Carroll County, Missouri      8564      54967    43.7
2  29143     New Madrid County, Missouri     16658      42370    41.7
3  29087           Holt County, Missouri      4295      52500    48.0
4  29113        Lincoln County, Missouri     58689      76622    37.5
5  29019          Boone County, Missouri    182170      62653    31.9
6  29099      Jefferson County, Missouri    225913      71285    39.9
7  29207       Stoddard County, Missouri     28812      46052    41.3
8  29189      St. Louis County, Missouri   1001982      72562    40.3
9  29195         Saline County, Missouri     23278      49705    36.3
10 29031 Cape Girardeau County, Missouri     81086      59252    36.7
                         geometry
1  MULTIPOLYGON (((-93.75918 3...
2  MULTIPOLYGON (((-89.97129 3...
3  MULTIPOLYGON (((-95.55482 4...
4  MULTIPOLYGON (((-91.26382 3...
5  MULTIPOLYGON (((-92.57016 3...
6  MULTIPOLYGON (((-90.77355 3...
7  MULTIPOLYGON (((-90.25976 3...
8  MULTIPOLYGON (((-90.73643 3...
9  MULTIPOLYGON (((-93.49638 3...
10 MULTIPOLYGON (((-89.8668 37...
Code
#remove that trailing "E"
colnames(mo_counties_withgeo) <- sub("E$", "", colnames(mo_counties_withgeo)) # $ means end of string only

mo_counties_withgeo
Simple feature collection with 115 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -95.7747 ymin: 35.99568 xmax: -89.09884 ymax: 40.61364
Geodetic CRS:  NAD83
First 10 features:
   GEOID                             NAM totalpop medincome medage
1  29033        Carroll County, Missouri     8564     54967   43.7
2  29143     New Madrid County, Missouri    16658     42370   41.7
3  29087           Holt County, Missouri     4295     52500   48.0
4  29113        Lincoln County, Missouri    58689     76622   37.5
5  29019          Boone County, Missouri   182170     62653   31.9
6  29099      Jefferson County, Missouri   225913     71285   39.9
7  29207       Stoddard County, Missouri    28812     46052   41.3
8  29189      St. Louis County, Missouri  1001982     72562   40.3
9  29195         Saline County, Missouri    23278     49705   36.3
10 29031 Cape Girardeau County, Missouri    81086     59252   36.7
                         geometry
1  MULTIPOLYGON (((-93.75918 3...
2  MULTIPOLYGON (((-89.97129 3...
3  MULTIPOLYGON (((-95.55482 4...
4  MULTIPOLYGON (((-91.26382 3...
5  MULTIPOLYGON (((-92.57016 3...
6  MULTIPOLYGON (((-90.77355 3...
7  MULTIPOLYGON (((-90.25976 3...
8  MULTIPOLYGON (((-90.73643 3...
9  MULTIPOLYGON (((-93.49638 3...
10 MULTIPOLYGON (((-89.8668 37...

Mapping Missouri counties with mapview

Code
mapview(mo_counties_withgeo, zcol = "medincome")

Customize colors

Code
mapview(mo_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (114)! 
Interpolating color vector to match number of zcol values.

Two maps together

Code
map_income <- mapview(mo_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (114)! 
Interpolating color vector to match number of zcol values.
Code
map_age <- mapview(mo_counties_withgeo, zcol = "medage", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (83)! 
Interpolating color vector to match number of zcol values.
Code
sync(map_income, map_age)

Side-by-side slider to compare the two, from the leaflet.extras2 package

Code
map_income | map_age

Turning off legends, hover text, popups…

Code
mapview(mo_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1,
         legend = FALSE, 
         label = FALSE, 
         popup = FALSE)
Warning: Found less unique colors (9) than unique zcol values (114)! 
Interpolating color vector to match number of zcol values.

My custom labels

Code
mylabel <- glue::glue("{mo_counties_withgeo$NAM} {mo_counties_withgeo$medincome}")


mapview(mo_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1,
         label = mylabel)
Warning: Found less unique colors (9) than unique zcol values (114)! 
Interpolating color vector to match number of zcol values.

Customize popups

Code
mypopup <- glue::glue("<strong>{mo_counties_withgeo$NAM}</strong><br />
                      Total Population: {mo_counties_withgeo$totalpop}<br />
                      Median Income: {mo_counties_withgeo$medincome}") %>% 
  lapply(htmltools::HTML)
Code
head(mypopup)
[[1]]
<strong>Carroll County, Missouri</strong><br />
Total Population: 8564<br />
Median Income: 54967

[[2]]
<strong>New Madrid County, Missouri</strong><br />
Total Population: 16658<br />
Median Income: 42370

[[3]]
<strong>Holt County, Missouri</strong><br />
Total Population: 4295<br />
Median Income: 52500

[[4]]
<strong>Lincoln County, Missouri</strong><br />
Total Population: 58689<br />
Median Income: 76622

[[5]]
<strong>Boone County, Missouri</strong><br />
Total Population: 182170<br />
Median Income: 62653

[[6]]
<strong>Jefferson County, Missouri</strong><br />
Total Population: 225913<br />
Median Income: 71285
Code
mapview(mo_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1,
         popup = mypopup, label = mylabel)
Warning: Found less unique colors (9) than unique zcol values (114)! 
Interpolating color vector to match number of zcol values.